package mynote;

import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

//写作界面弹出菜单监听
public class TextPopupListener extends MouseAdapter {
    JPopupMenu popup;
    Clipboard clipboard;
    JMenuItem copy;
    JMenuItem paste;
    JTextArea textArea;

    public TextPopupListener(JPopupMenu popup, Clipboard clipboard, JMenuItem copy, JMenuItem paste, JTextArea textArea) {
        this.popup = popup;
        this.clipboard = clipboard;
        this.copy = copy;
        this.paste = paste;
        this.textArea = textArea;
    }

    public void mouseReleased(MouseEvent e) {
        if (e.isPopupTrigger()) {
            //根据剪贴板内容激活/禁用选项
            copy.setEnabled(textArea.getSelectedText() != null);
            paste.setEnabled(clipboard.getContents(null) != null);
            popup.show(e.getComponent(), e.getX(), e.getY());
        }
    }
}
